﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameXNA_Master
{
    class MainMenu : Menu
    {
        public enum voice { Riprendi, Nuova_Partita, Carica, Opzioni, Punteggi, Esci };
        public string[] voiceText = new string[] { "RIPRENDI", "NUOVA PARTITA", "CARICA", "OPZIONI", "VEDI COMANDI", "ESCI" };
        public int voiceSelected = 0;

        public Texture2D mainMenuTexture;
        public Texture2D selectionTexture2;

        public int nrVoices = Enum.GetNames(typeof(voice)).Length;



        int selectedVoice = 1;
        public Vector2 starPosition;
        SpriteFont menufont;
        Rectangle[] rectangleSource = new Rectangle[] { new Rectangle(9, 16, 96, 74), new Rectangle(104, 16, 102, 74) };
        float ms = 0;
        int k = 0;



        RenderingData rendering_data;
        InputService input_service;

        public event Action iMoveUp_Menu;
        public event Action iMoveDown_Menu;
        public event Action iMoveLeft_Menu;
        public event Action iMoveRight_Menu;
        public event Action iConfirm_Menu;

        public MainMenu(Game game)
        {
            this.input_service = (InputService)game.Services.GetService(typeof(InputService));
            this.selectionTexture2 = game.Content.Load<Texture2D>(@"Menu\InvadersAnim2D");
            this.mainMenuTexture = game.Content.Load<Texture2D>(@"Menu\space-invaders-extreme-01");
            this.rendering_data = (RenderingData)game.Services.GetService(typeof(RenderingData));
            this.menufont = game.Content.Load<SpriteFont>("matrix");

            iMoveUp_Menu += () =>
            {

                if (!gameInfoState.some_To_Loading && selectedVoice == (int)voice.Opzioni)
                    selectedVoice -= 2;
                else
                {
                    if (selectedVoice == (int)voice.Nuova_Partita && !gameInfoState.pause_from_game)
                    {
                        selectedVoice = nrVoices - 1;
                    }
                    else
                    {
                        if (selectedVoice == 0)

                            selectedVoice = nrVoices - 1;

                        else
                        {
                            selectedVoice--;
                        }
                    }
                }

            };

            iMoveDown_Menu += () =>
            {

                if (!gameInfoState.some_To_Loading && selectedVoice == (int)voice.Nuova_Partita) // se sono su Nuova Partita
                    selectedVoice += 2;
                else
                {
                    if (selectedVoice == (int)voice.Esci && !gameInfoState.pause_from_game)
                    {
                        selectedVoice = 1;
                    }
                    else
                    {
                        if (selectedVoice == nrVoices - 1)

                            selectedVoice = 0;
                        else
                        {
                            selectedVoice++;
                        }
                    }

                }

            };

            iConfirm_Menu += () =>
            {
                menu_selezionato = setSelectedMenu();
            };
        }


        public void MoveUp()
        {
            if (iMoveUp_Menu != null)
                iMoveUp_Menu();
        }
        public void MoveDown()
        {
            if (iMoveUp_Menu != null)
                iMoveDown_Menu();
        }
        public void MoveLeft()
        {
            if (iMoveLeft_Menu != null)
                iMoveLeft_Menu();
        }
        public void MoveRight()
        {
            if (iMoveRight_Menu != null)
                iMoveRight_Menu();
        }
        public void ConfirmAction()
        {
            if (iConfirm_Menu != null)
                iConfirm_Menu();
        }


        private int setSelectedMenu()
        {
            switch (selectedVoice)
            {
                case (int)voice.Riprendi:
                    input_service.Pause_Game();
                    selectedVoice = 1;
                    return 0;
                case (int)voice.Nuova_Partita:
                    gameInfoState.pause_from_game = true;
                    selectedVoice = 1;
                    input_service.New_Game();
                    return 0;
                case (int)voice.Carica:
                    gameInfoState.pause_from_game = true;
                    input_service.Load_Game();
                    return 0;
                case (int)voice.Opzioni:
                    return 1;
                case (int)voice.Punteggi:
                    return 2;
                case (int)voice.Esci:
                    input_service.Exit_Game();
                    return 0;
                default: return 0;
            }

        }

        int menu_selezionato = 0;
        public void Update(GameTime gameTime, out int SelectedMenu)
        {
            ms += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (ms >= 0.5f) //0.08f
            {
                k++;
                ms = 0;
            }

            SelectedMenu = menu_selezionato;
            menu_selezionato = 0;

            switch (selectedVoice)
            {
                case 0:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(58, 34), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }

                case 1:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(58, 44), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }
                case 2:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(58, 54), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);

                        break;
                    }
                case 3:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(58, 64), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }
                case 4:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(58, 74), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);

                        break;
                    }
                case 5:
                    {
                        starPosition = Vector2.Lerp(starPosition, new Vector2(58, 84), (float)gameTime.ElapsedGameTime.TotalSeconds * 5f);
                        break;
                    }
                default: break;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null, null, Matrix.CreateScale(rendering_data.screenWidth / 100.0f, rendering_data.screenHeight / 100.0f, 1.0f));

            spriteBatch.Draw(mainMenuTexture, new Rectangle(0, 0, 100, 100), null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0f);

            spriteBatch.Draw(selectionTexture2, starPosition, rectangleSource[k % 2], Color.Yellow, 0f, Vector2.Zero, Vector2.One * 0.08f, SpriteEffects.None, 0);

            Vector2 temp = new Vector2(67, 25);

            for (int i = 0; i < voiceText.Length; i++)
            {
                string s = voiceText[i];
                Color color = (i == selectedVoice) ? Color.Orange : Color.White;
                spriteBatch.DrawString(menufont, s, temp += new Vector2(0, 10), (!gameInfoState.some_To_Loading && s.Equals("CARICA")) ? Color.Gray : color, 0, Vector2.Zero, 0.07f, SpriteEffects.None, 0f);
            }
            spriteBatch.End();
        }
    }

}
